import requests
from flask import session
from selenium import webdriver

from util.constant import Constant
from util.request_helper import get_image_path


def request_restaurant_list(cookie, latitude, longitude, offset):
    headers = {
        "accept": "application/json, text/plain, */*",
        "referer": "https://h5.ele.me/msite/food/",
        "sec-fetch-dest": "empty",
        "sec-fetch-mode": "cors",
        "sec-fetch-site": "same-origin",
        'user-agent': 'Mozilla/5.0 (iPhone; CPU iPhone OS 13_2_3 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/13.0.3 Mobile/15E148 Safari/604.1',
        'x-shard': 'loc=' + str(longitude) + ',' + str(latitude),
        'x-ua': 'RenderWay/H5 AppName/wap',
        'x-uab': '124#4EuJ2JztxG0xAPcjR0c7HeKVVpRFx4SskGO1Me+5ZXguXA0v3UtKPvNmSMx+Zk+hswFEEFeDtxEXu2TOmxnkyZRY5nrxzND3ysE3Vn/sUUswJlNDK33AqOYC4qPmUOTH4GOY4X6BnuhZkZrSWwkwLigMKS02lTj/4QuGkDRRZRw/K01G8kUxInYLlTYIm4WZIqXLgTzd1Z2ebUL2gKvm/UwT5wiJm4afI8LpgTG41nIelUX2g7vtIZ/Llm/Jm4WZIqXLg5Sd1Z2ZlMLng7OBInYLlwYnmfWxJMbBgmUd1qyelULnm6tu0U9LUTY2USAeIqXpgxMTxilGvwi5uEqNRldbhEM2fFlnzJ1/a/vtDwL4Q0LQrMxN76oGQj3TplEcoyFbjMd4AJFl4Udi+O61SJ9Dv2tYxwu5+Dl3p7MlBSBmHG1tpBy82VTcBoLVLz1Rm6Xi7tnrwu7fQrhTxQYUzkWuxz1g4Bc5eaqQmP5QI2WHO12JVJ5Ye0QGmAuaRzipiKcOu3GqUhvXZ+1I1ua6HWwcBcUToyAqC2vYD+QNd0fPRSROWVAwJ1mRlGX7uGgswmaQJOvBOtB1Xx7P/QbLg/tRL5W4ubLMoJ0B4QaRhzA0/RCGtqAAHWnH1gjYoQQz/NddhdTleWJfr3oevsA7OuBeICYaBf9kIrlV9EdMAnt8wvZFkucmubVI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GnY+nI93MzXgfzUvGGupPNEBGZd+Z5qaee56vzi97Pdiw1QTrKMgRcj0RXCET8GovOA3N48fdH0YQ7v/FWDntg7oXp++Qs2JyFU6NNewmg+YiQ1ADY4V4RiZtnayFnlh0Zrz3od3M0mnI/44dVw4A8Hl//QL5btwnEPBYQRsISCFIetxBOip90X9TN+c+dAKnMPJtFyPt+MJOu1i0aK+UyV+n76RELdxLN0YkY40Rn4HTFgiNjXcXvRrh7r0OnMtzSsNzFbAOGJeQi4aBysbxRNp7QBILMme8HZ1ih8ERV'
    }
    factor_params = {
        "longitude": longitude,
        "latitude": latitude,
        "keyword": "",
        "offset": offset,
        "limit": 16,
        "extras": ["activities", "tags"],
        "terminal": "h5"
    }
    result = {}
    sess = requests.Session()
    restaurant_request = sess.get(Constant.RESTAURANT_REQUEST_URL, params=factor_params, headers=headers, cookies=cookie)
    response = restaurant_request.json()
    # 出现频繁请求IP被拦截
    if 'rgv587_flag' in response:
        return None
    else:
        restaurants = restaurant_request.json()['items']
        result['has_next'] = restaurant_request.json()['has_next']
        restaurant_list = []
        for res in restaurants:
            r = res['restaurant']
            restaurant_list.append({
                'restaurant_id': r['id'],
                'restaurant_name': r['name'],
                'rating': r['rating'],
                'restaurant_image': get_image_path(r['image_path']),
                'recent_order_num': r['recent_order_num'],
                'order_lead_time': r['order_lead_time'],
                'float_minimum_order_amount': r['float_minimum_order_amount'],
            })
        result['restaurants'] = restaurant_list
        return result
